﻿namespace PlugNT.Cms.Core
{
    using PlugNT.Cms.BLL;
    using PlugNT.Custom;
    using PlugNT.Database.Common.Complex;
    using Simple = PlugNT.Database.Common.Simple;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;

    public class DataAccess
    {
        private static string _copyrightString = null;
        public static readonly Article curArticle = new Article();
        public static readonly Channel curChannel = new Channel();
        public static readonly FriendLink curFriendLink = new FriendLink();
        public static readonly GroupAdmin curGroupAdmin = new GroupAdmin();
        public static readonly Templet curTemplet = new Templet();
        public static readonly ProductClass curProductClass = new ProductClass();
        public static void CreateSqlDatabase(string dbType, string dbConstring, string sqlString)
        {
            DbInfo dbinfo = new DbInfo(dbType, dbConstring);
            DbHelper.ExecuteSql(dbinfo, sqlString);
        }

        internal static DataTable GetContentTable(string sqlkey)
        {
            if (WebConfig.SqlKeyValues.ContainsKey(sqlkey))
            {
                return Simple.DbHelper.TabQuery(WebConfig.SqlKeyValues[sqlkey]);
            }
            return new DataTable();
        }

        internal static DataTable GetContentTable(int idvalue, string postfix)
        {
            return ((idvalue > 0) ? curArticle.GetList(idvalue, postfix) : null);
        }

        internal static DataTable GetContentTable(string sqlkey, params string[] args)
        {
            if (WebConfig.SqlKeyValues.ContainsKey(sqlkey))
            {
                return Simple.DbHelper.TabQuery(string.Format(WebConfig.SqlKeyValues[sqlkey], (object[])args));
            }
            return new DataTable();
        }

        internal static DataTable GetContentTable(int topCount, string clsno, string type, string sort, string postfix, bool isexact)
        {
            return curArticle.GetList(topCount, clsno, type, sort, postfix, isexact);
        }

        public static string GetCopyright()
        {
            if (_copyrightString == null)
            {
                _copyrightString = "<span class=\"copyright\">Powered by <strong><a href=\"http://www.yibocheng.com\" target=\"_blank\">艺博城 " + WebConfig.Version + "</strong></a></span>";
            }
            return _copyrightString;
        }
        internal static string GetProductCatory(string clsno)
        {
            return curProductClass.GetProductCatory1(clsno);
        }
        internal static string GetFriendLink(string clsno, string splitstr)
        {
            return curFriendLink.CreateLinks(clsno, splitstr);
        }

        internal static DataTable GetListContentTable(int pagesize, int currpage, string type, string sort, string andwhere, string postfix, out int recordCount)
        {
            return curArticle.GetList(pagesize, currpage, type, sort, andwhere, postfix, out recordCount);
        }

        internal static string GetNavigation(string clsno)
        {
            return curChannel.GetNavigation(clsno);
        }

        public static bool ReSetTable(string tabName, string idName)
        {
            return Simple.DbHelper.ReSetTable(tabName, idName);
        }
    }
}

